﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;

namespace GameSchool.Models.ExamModels
{
    public class ExamModel
    {
        public ExamModel()
        {
            ExamQuestions = new List<ExamQuestionModel>();
        }

        /// <summary>
        /// Mappar Exam í Entity módelinu yfir í ExamModel DTO klasann
        /// </summary>
        /// <param name="examEntity"></param>
        public ExamModel(Exam examEntity)
        {
            Name = examEntity.Name;
            Description = examEntity.Description;
            HoursToFinish = examEntity.HoursToFinish;
            GradePercentage = examEntity.GradePercentage * 100;
            ExamQuestions = new List<ExamQuestionModel>();
            foreach (var eq in examEntity.ExamQuestion)
            {
                ExamQuestions.Add(new ExamQuestionModel(eq));
            }

        }

        public string Name { get; set; }
        public string Description { get; set; }
        public int HoursToFinish { get; set; }
        public double GradePercentage { get; set; }
        public List<ExamQuestionModel> ExamQuestions { get; set; }

        /// <summary>
        /// Mappar ExamModel DTO klasann yfir í Entity módel
        /// </summary>
        /// <returns></returns>
        public Models.Exam ToExamEntity()
        {
            var exam = new Exam();
            exam.Name = this.Name;
            exam.Description = this.Description;
            exam.HoursToFinish = this.HoursToFinish;
            exam.GradePercentage = this.GradePercentage / 100;
            exam.ExamQuestion = new EntityCollection<ExamQuestion>();

            foreach (var eqm in ExamQuestions)
            {
                var q = new ExamQuestion();
                q.Question = eqm.ExamQuestion;
                q.ExamQuestionAnswers = new EntityCollection<ExamQuestionAnswers>();
                q.ExamQuestionAnswers.Add(new ExamQuestionAnswers()
                                              {
                                                  Answer = eqm.ExamQuestionAnswer1,
                                                  IsCorrect = eqm.CorrectAnswerIsNo == 1 ? true : false
                                              });
                q.ExamQuestionAnswers.Add(new ExamQuestionAnswers()
                                              {
                                                  Answer = eqm.ExamQuestionAnswer2,
                                                  IsCorrect = eqm.CorrectAnswerIsNo == 2 ? true : false
                                              });
                q.ExamQuestionAnswers.Add(new ExamQuestionAnswers()
                                              {
                                                  Answer = eqm.ExamQuestionAnswer3,
                                                  IsCorrect = eqm.CorrectAnswerIsNo == 3 ? true : false
                                              });
                q.ExamQuestionAnswers.Add(new ExamQuestionAnswers()
                                              {
                                                  Answer = eqm.ExamQuestionAnswer4,
                                                  IsCorrect = eqm.CorrectAnswerIsNo == 4 ? true : false
                                              });
                exam.ExamQuestion.Add(q);
            }

            return exam;
        }
    }
}